package com.blb.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.Annotation.Log;
import com.blb.community.entity.ZyBuilding;
import com.blb.community.entity.ZyCommunity;
import com.blb.community.entity.vo.BuildingVo;
import com.blb.community.service.IZyBuildingService;
import com.blb.community.service.IZyCommunityService;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 楼栋  前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "楼栋信息管理模块")
@RestController
@RequestMapping("/zy-building")
public class ZyBuildingController {

    @Autowired
    private IZyBuildingService buildingService;

    @GetMapping("builds")
    public ResponseResult<List<ZyBuilding>> buildsList() {
        return ResponseResult.ok(buildingService.list());
    }


    @Autowired
    private IZyCommunityService communityService;

    @ApiOperation("分页+模糊查询楼栋列表")
    @GetMapping("/buildings/page")
    public ResponseResult<IPage<ZyBuilding>> selectBuildingPage(BuildingVo buildingVo) {
        IPage<ZyBuilding> zyBuildingIPage = buildingService.selectBuildingPage(new Page<>(buildingVo.getCurrent(), 5L), buildingVo);
        return ResponseResult.ok(zyBuildingIPage);
    }

    @ApiOperation("获取社区信息")
    @GetMapping("/communitys")
    public ResponseResult<List<ZyCommunity>> selectCommunitys() {
        List<ZyCommunity> list = communityService.list();
        return ResponseResult.ok(list);
    }

    @ApiOperation("分页+模糊查询不同小区楼栋列表")
    @GetMapping("/buildings/building")
    public ResponseResult<IPage<ZyBuilding>> selectBuilding(BuildingVo buildingVo) {
        IPage<ZyBuilding> zyBuildingIPage = buildingService.selectBuildingPage(new Page<>(buildingVo.getCurrent(), 5L), buildingVo);
        return ResponseResult.ok(zyBuildingIPage);
    }

    @ApiOperation("添加楼栋信息")
    @PostMapping("/building")
    @Log(module = "添加楼栋信息", operationType = 1, requestMethod = "POST")
    public ResponseResult<String> addBuilding(ZyBuilding zyBuilding) {
        String buildingName = zyBuilding.getBuildingName();
        List<ZyBuilding> list = buildingService.list();
        for (int i = 0; i < list.size(); i++) {
            if (buildingName.equals(list.get(i).getBuildingName())) {
                return ResponseResult.ok("楼栋名重复");
            }
        }
        buildingService.save(zyBuilding);
        return ResponseResult.ok("添加成功");
    }


    @Log(module = "修改楼栋信息", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改楼栋信息")
    @PutMapping("/building")
    public ResponseResult<String> updateBuilding(@RequestBody ZyBuilding zyBuilding) {
        buildingService.updateById(zyBuilding);
        return ResponseResult.ok("修改成功");
    }

    @Log(module = "删除楼栋", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除楼栋")
    @DeleteMapping("/building/{buildingId}")
    public ResponseResult<String> deleteBuilding(@PathVariable Long buildingId) {
        buildingService.removeById(buildingId);
        return ResponseResult.ok("删除成功");
    }


    @Log(module = "批量删除楼栋信息", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("批量删除楼栋信息")
    @DeleteMapping("/buildings/{arr}")
    public ResponseResult<String> deleteBuildings(@PathVariable ArrayList<Long> arr) {
        buildingService.removeByIds(arr);
        return ResponseResult.ok("批量删除成功");
    }
}
